﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
using Common.Logging;
using In.Api.Dto.HisWs;
using Newtonsoft.Json;
using OutDbContext;
using User.Api.Dto.Report;
using User.Api.HttpClients;

namespace User.Api.Controllers
{
    public class ReportController : ApiController
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(ReportController));
        private FsssdqfybjyEntities _ctx;

        protected override void Initialize(HttpControllerContext controllerContext)
        {
            _ctx = new FsssdqfybjyEntities();
            base.Initialize(controllerContext);
        }

        protected override void Dispose(bool disposing)
        {
            _ctx.Dispose();
            base.Dispose(disposing);
        }

        [HttpGet]
        [ActionName("GetReportList")]
        public GetReportListResponse GetReportList(string cardNo, string cardName, string examType, string startDate)
        {
            var request = new Request4GetExamInfo();
            request.CardNo = cardNo;
            request.ExamType = examType;
            request.StartDate = startDate;
            request.EndDate = startDate.ToDateTimeOrDefault().AddDays(16).ToString("yyyy-MM-dd");

            Response4GetExamInfo response;
            try
            {
                var httpClient = InHttpClient.GetClient();
                var responseMsg = httpClient.PostAsJsonAsync("HisWs/GetExamInfo", request).Result;
                responseMsg.EnsureSuccessStatusCode();
                response = responseMsg.Content.ReadAsAsync<Response4GetExamInfo>().Result;
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用院内获取检查检验单列表Api出错,信息：{0}", JsonConvert.SerializeObject(ex));

                return new GetReportListResponse
                {
                    ResultCode = "2",
                    ResultMsg = "获取取检查检验单列表超时"
                };
            }

            if (response.ResultCode != "0")
            {
                return new GetReportListResponse
                {
                    ResultCode = "2",
                    ResultMsg = response.ResultDesc
                };
            }

            return new GetReportListResponse
            {
                ResultCode = "0",
                ResultMsg = "",
                ResultData = response.ExamInfoList.OrderByDescending(x => x.OperDate).Select(x => new GetReportListResponse.Report
                {
                    ExamId = x.ExamId,
                    ExamType = x.ExamType,
                    OperDate = x.OperDate,
                    OperDocName = x.OperDocName,
                    OperDocDept = x.OperDocDept,
                    ExamItem = x.ExamItem,
                    CardNo = x.CardNo,
                    Name = x.Name,
                    Sex = x.Sex,
                    ReportDate = x.ReportDate
                }).ToList()
            };
        }

        [HttpGet]
        [ActionName("GetReportDetail")]
        public GetReportDetailResponse GetReportDetail(string cardNo, string cardName, string examType, string examId)
        {
            var request = new Request4GetExamDetail();
            request.CardNo = cardNo;
            request.ExamId = examId;
            request.ExamType = examType;

            Response4GetExamDetail response;
            try
            {
                var httpClient = InHttpClient.GetClient();
                var responseMsg = httpClient.PostAsJsonAsync("HisWs/GetExamDetail", request).Result;
                responseMsg.EnsureSuccessStatusCode();
                response = responseMsg.Content.ReadAsAsync<Response4GetExamDetail>().Result;
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("调用院内获取检查检验单详情Api出错,信息：{0}", JsonConvert.SerializeObject(ex));

                return new GetReportDetailResponse
                {
                    ResultCode = "2",
                    ResultMsg = "获取化验单列表超时"
                };
            }

            if (response.ResultCode != "0")
            {
                return new GetReportDetailResponse
                {
                    ResultCode = "2",
                    ResultMsg = response.ResultDesc
                };
            }

            return new GetReportDetailResponse
            {
                ResultCode = "0",
                ResultMsg = "",
                ResultData = response.ExamDetailInfoList.Select(x => new GetReportDetailResponse.Detail
                {
                    ExamId = x.ExamId,
                    ExamType = x.ExamType,
                    ExamItem = x.ExamItem,
                    ReportDate = x.ReportDate,
                    Report = x.Report,
                    Conclusion = x.Conclusion,
                    DetailItem = x.DetailItem,
                    Result = x.Result,
                    Unit = x.Unit,
                    Alert = x.Alert,
                    Rang = x.Rang
                }).ToList()
            };
        }
    }
}
